package com.amazon.jobtest.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.amazon.jobtest.Student;
import com.amazon.jobtest.TestResult;
import com.amazon.jobtest.exception.BRException;

public class CsvUtil {
	private static final int CVS_DATE_COLUMN = 0;
	private static final int CVS_SCORE_COLUMN = 1;
	private static final int CVS_STUDENT_COLUMN = 2;
	private static final String DEFAULT_COLUMN_SEPARATOR = ",";
	private static final String DATABASE_DATE_PATTERN = "yyyy-MM-dd";

	/**
	 * Transforms into a list of Student a csv file with the format:
	 * ["testDate", testScore, "studentId"] the date should be on the format
	 * of the DATABASE_DATE_PATTERN
	 * 
	 * @param csvFile An file instance for the csv to be read
	 * @return returns a list of students and their tests
	 */

	public static List<Student> readCsv(File csvFile) {
		return readCsv(csvFile, DATABASE_DATE_PATTERN, DEFAULT_COLUMN_SEPARATOR);
	}

	
	/**
	 * Transforms into a list of Student a csv file 
	 * @param csvFile An file instance for the csv to be read
	 * @param datePattern the pattern for the date fields
	 * @param columnSeparator the csv column separator character
	 * @return returns a list of students and their tests
	 */

	public static List<Student> readCsv(File csvFile, String datePattern, String columnSeparator) {
		//TODO see: populate student as an ArrayList is the best choice? Not sure..
		List<Student> studentList = new ArrayList<Student>();
		BufferedReader br = null;
		String line = "";
		try {
			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {
				// use column separator
				String[] testLine = line.split(columnSeparator);
				Student student = lineToStudent(testLine, datePattern);
				// if the list does not contain this student
				int studentIndex = studentList.indexOf(student);
				if (studentList.indexOf(student) < 0) {
					studentList.add(student);
				} else {
					studentList.get(studentIndex).getTestResultList()
							.addAll(student.getTestResultList());
				}
			}	
		// TODO learn more about system usage to throw this
		} catch (FileNotFoundException e) {
			throw new BRException(e);
		} catch (IOException e) {
			throw new BRException(e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					throw new BRException(e);
				}
			}
		}
		return studentList;
	}
	/**
	 * Transforms and string array into a Student object based on the constants CVS_*_COLUMN  
	 * @param testLine
	 * @param datePattern
	 * @return Student an student object with a test
	 */
	private static Student lineToStudent(String[] testLine, String datePattern) {
		Student student = new Student();
		student.setStudentId(testLine[CVS_STUDENT_COLUMN]);
		Date date = DateUtil.stringToDate(testLine[CVS_DATE_COLUMN], datePattern);
		float score = Float.valueOf(testLine[CVS_SCORE_COLUMN]);
		TestResult test = new TestResult(date, score);
		student.addTestResult(test);
		return student;
	}
}
